package com.core.model.user;

import com.core.dto.UserDTO;
import com.core.model.AbstractEntity;
import org.hibernate.annotations.*;
import org.hibernate.annotations.Cache;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: sunk
 * Date: 14-12-26
 * Time: 下午4:30
 * To change this template use File | Settings | File Templates.
 */
@Entity
@Table(name = "user")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
//@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public abstract class User extends AbstractEntity implements UserDetails {
    @Column(name = "username")
    private String username;

    @Column(name = "password")
    private String password;

    @Column(name = "is_activate")
    private String isActivate;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "userInfo_id")
    private UserInfo userInfo;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "user_id")
    protected List<UserRole> roles = new ArrayList<>();

    public User() {
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    protected User(String username, String password, String activate) {
        this.username = username;
        this.password = password;
        this.isActivate = activate;
    }

    /**
     * set Role
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<GrantedAuthority> authorities = new HashSet<>();
        for (UserRole role : roles) {
            List<GrantedAuthority> roleAuthorities = role.getAuthorities();
            authorities.addAll(roleAuthorities);
        }
        return authorities;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String isActivate() {
        return isActivate;
    }

    public void setActivate(String activate) {
        this.isActivate = activate;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.isActive();
    }

    public boolean isCustomer() {
        return false;
    }

    public boolean isAdministrator() {
        return false;
    }

    public UserInfo userInfo() {
        return userInfo;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void initUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    public void initUserRoles(List<UserRole> roles) {
        this.roles = roles;
    }

    public List<UserRole> roles() {
        return roles;
    }
}
